package org.xhy.circuitbreaker;

/**
 * @description:
 * @gitee: https://gitee.com/XhyQAQ
 * @copyright: B站: https://space.bilibili.com/152686439
 * @Author: Xhy
 * @CreateTime: 2024-06-04 22:19
 */
public class RequestRateCalculator {

    private Integer successCount = 0;

    private Integer failCount = 0;


    public Integer getSuccessCount() {
        return successCount;
    }

    public void setSuccessCount(Integer successCount) {
        this.successCount = successCount;
    }

    public Integer getFailCount() {
        return failCount;
    }

    public void setFailCount(Integer failCount) {
        this.failCount = failCount;
    }

    public void incrSuccess(){
        this.successCount++;
    }
    public void incrFail(){
        this.failCount++;
    }

    // 计算请求失败比例
    public boolean isSuccessRateWithinLimit(double failRateLimit){
        int totalRequests = successCount + failCount;
        double actualSuccessRate = (double) failCount  / totalRequests;
        return actualSuccessRate >= failRateLimit;
    }

    public void reset(){
        this.successCount = 0;
        this.failCount = 0;
    }
}
